package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.pojo.vo.CompanyInfoVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.service.CompanyInfoService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 公司信息
 * @author 李少杰
 * @version v1.0.0
 * @date 2022/3/3 23:04
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/mobile/company")
public class CompanyInfoController {

    @Resource
    private CompanyInfoService companyInfoService;

    /**
     * 获取公司信息
     * @param baseDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/info", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String info(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) {
        XbbResponse<CompanyInfoVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CompanyInfoVO companyInfo = companyInfoService.getCompanyInfo(baseDTO);
                response = new XbbResponse<>(companyInfo);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
